package com.service.impl;

import com.common.exception.db.DeleteException;
import com.common.exception.db.InsertException;
import com.common.exception.db.UpdateException;
import com.dao.LocationDao;
import com.dao.StudentDao;
import com.entity.StudentInfo;
import com.entity.SubjectInfo;
import com.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentDao studentDao;
    @Autowired
    private LocationDao dao;

    @Override
    @Transactional
    public int publish(Integer userId,String studentName, String studentSex, String teachStage, String counselingMethod, Integer cityId, Integer locationId, String adress, String classTime, String subjects, String teacherSex, String salary, String overview) {
        int count = studentDao.publish(userId,studentName, studentSex, teachStage, counselingMethod, cityId, locationId, adress, classTime, subjects, teacherSex, salary, overview);
        if(count > 0){
            String[] sub = subjects.split(",");
            for (int i = 0; i < sub.length; i++) {
                int res = studentDao.addUserSubject(userId,sub[i]);
                if(res == 0){
                    throw new InsertException("新增失败");
                }
            }
            return count;
        }else{
            throw new InsertException("新增失败");
        }
    }

    @Override
    public int findPublishCount(Integer userId) {
        return studentDao.findPublishCount(userId);
    }

    @Override
    public StudentInfo findPublish(Integer userId) {
        StudentInfo stu = studentDao.findPublish(userId);
        if(stu != null){
            List<SubjectInfo> subject = studentDao.findSubject(userId);
            stu.setSubjectInfo(subject);
        }else{
            return null;
        }

        return stu;
    }

    @Override
    @Transactional
    public int deletePublish(Integer id, Integer userId,Integer count) {
        int stu=studentDao.deletePublish(id);
        if(stu>0){
            int stu2=studentDao.deleteUserSubject(userId);
            if(stu2>0){
                if(count>0){
                    int stu3=studentDao.deleteAssociation(userId);
                    if(stu3>0){
                        return stu3;
                    }else {
                        throw new DeleteException("删除失败");
                    }
                }else {
                    return stu2;
                }
            }else {
                throw new DeleteException("删除失败");
            }
        }else {
            throw new DeleteException("删除失败");
        }
    }

    @Override
    public List<StudentInfo> seekStudent(String cityName,Integer locationId, Integer subjectId, String teachStage, Integer limit) {
        String asdd = "";
        for (int a = 0; a < cityName.length(); a++) {
            asdd = String.valueOf(cityName.charAt(a));
            if (asdd.equals("市")) {
                cityName = cityName.replace("市", "");
            }
        }
        Integer cityId = dao.findCityIsBeing(cityName);
        if (cityId != null) {
            if(subjectId!=0){
                List<StudentInfo> list=studentDao.seekStudent(cityId,locationId,subjectId,teachStage,limit);
                for(StudentInfo studentInfo : list){
                    List<SubjectInfo> sub=studentDao.findSub(studentInfo.getUserId());
                    studentInfo.setSubjectInfo(sub);
                }
                return list;
            }else {
                List<StudentInfo> list2=studentDao.seekStudent2(cityId,locationId,teachStage,limit);
                for(StudentInfo studentInfo : list2){
                    List<SubjectInfo> sub=studentDao.findSub(studentInfo.getUserId());
                    studentInfo.setSubjectInfo(sub);
                }
                return  list2;
            }
        }else{
            return null;
        }
    }

    @Override
    public StudentInfo findStudentDetail(Integer userId) {
        StudentInfo info = studentDao.findStudentDetail(userId);
        List<SubjectInfo> sub = studentDao.findSub(userId);
        info.setSubjectInfo(sub);
        return info;
    }


    @Override
    @Transactional
    public int updatePublish(Integer userId,String studentName, String studentSex, String teachStage, String counselingMethod, Integer cityId, Integer locationId, String adress, String classTime, String subjectId, String teacherSex, String salary, String overview) {
        int stu=studentDao.updatePublish(userId,studentName,studentSex,teachStage,counselingMethod,cityId,locationId,adress,classTime,teacherSex,salary,overview);
        if(stu > 0){
            int stu1 = studentDao.deleteUserSub(userId);
            if(stu1 > 0){
                String[] split = subjectId.split(",");
                for  (String subId : split) {
                    int stu2 = studentDao.insertSub(userId, subId);
                    if(stu2 < 1){
                        throw new UpdateException("修改失败");
                    }
                }
                return stu1;
            }else {
                throw new UpdateException("修改失败");
            }
        }else{
            throw new UpdateException("修改失败");
        }
    }

    @Override
    public int findPublishState(Integer userId) {
        return studentDao.findPublishState(userId);
    }
}
